if __name__ == '__main__':
    # 外部启动django的方法
    import sys, os
    from django.core.wsgi import get_wsgi_application

    base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    sys.path.append(base_dir)
    os.environ['DJANGO_SETTINGS_MODULE'] = 'drf_movie_rimi.settings'
    application = get_wsgi_application()

    from rest_framework import serializers
    from mytest.models import Mytest



    class MyTestSerializer(serializers.Serializer):
        name = serializers.CharField(max_length=20)
        age = serializers.IntegerField()

        # 重写save方法 让他验证成功后保存数据
        def create(self, validated_data):
            return Mytest.objects.create(**validated_data)

        def validate(self, attrs):

            x = attrs
            return super().validate(attrs=attrs)

    data = MyTestSerializer(data={'name':1341})
    data.is_valid()
    data.save()




